package com.wongleoi.weather_demo

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.wongleoi.weather_demo.databinding.WeatherForecastItemLayoutBinding
import com.wongleoi.weather_demo.model.Weather
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*

class CityWeatherDetailAdapter(
    private val data: MutableList<Weather.WeatherInfo>,
    val context: Context
) :
    RecyclerView.Adapter<CityWeatherDetailViewHolder>() {

    val adapterData get() = data

    override fun onBindViewHolder(holder: CityWeatherDetailViewHolder, position: Int) {
        holder.bind(data[position])
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CityWeatherDetailViewHolder {
        return CityWeatherDetailViewHolder(
            WeatherForecastItemLayoutBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            ), context
        )
    }

    override fun getItemCount() = data.size

}

class CityWeatherDetailViewHolder(
    val viewBinding: WeatherForecastItemLayoutBinding,
    val context: Context
) :
    RecyclerView.ViewHolder(viewBinding.root) {
    fun bind(item: Weather.WeatherInfo) {
        viewBinding.run {
            tvForecastInfo.text =
                StringBuilder().append(item.date).append(" ").append(getWeek(item.date))
                    .append("   气温:").append(item.daytemp).append("/")
                    .append(item.nighttemp).append("℃")
            tvDayweather.text =
                String.format(context.getString(R.string.string_dayweather), item.dayweather)
            tvDaywind.text = String.format(context.getString(R.string.string_daywind), item.daywind)
            tvDaypower.text =
                String.format(context.getString(R.string.string_daypower), item.daypower)

            tvNightweather.text =
                String.format(context.getString(R.string.string_nightweather), item.nightweather)
            tvNightwind.text =
                String.format(context.getString(R.string.string_nightwind), item.nightwind)
            tvNightpower.text =
                String.format(context.getString(R.string.string_nightpower), item.nightpower)
        }

    }

    @SuppressLint("SimpleDateFormat")
    private fun getWeek(pTime: String): String {
        var Week = ""
        val format = SimpleDateFormat("yyyy-MM-dd")
        val c: Calendar = Calendar.getInstance()
        try {
            c.time = format.parse(pTime) as Date
            if (c.get(Calendar.DAY_OF_WEEK) == 1) {
                Week += context.getString(R.string.string_sunday)
            }
            if (c.get(Calendar.DAY_OF_WEEK) == 2) {
                Week += context.getString(R.string.string_monday)
            }
            if (c.get(Calendar.DAY_OF_WEEK) == 3) {
                Week += context.getString(R.string.string_tuesday)
            }
            if (c.get(Calendar.DAY_OF_WEEK) == 4) {
                Week += context.getString(R.string.string_wednesday)
            }
            if (c.get(Calendar.DAY_OF_WEEK) == 5) {
                Week += context.getString(R.string.string_thursday)
            }
            if (c.get(Calendar.DAY_OF_WEEK) == 6) {
                Week += context.getString(R.string.string_friday)
            }
            if (c.get(Calendar.DAY_OF_WEEK) == 7) {
                Week += context.getString(R.string.string_saturday)
            }
        } catch (e: ParseException) {
            e.printStackTrace()
        }
        return Week
    }


}